﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.doobers {
    import Engine.Managers.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.sim.*;
    import Modules.stats.experiments.*;
    import Engine.*;
    import com.adobe.utils.*;

    public class DooberManager implements IGameWorldStateObserver {

        private static var m_lastDooberBatchId:int = 0;

        private const DOOBER_RADIUS:Number = 25;
        private const DOOBER_ELLIPSE_A:Number = 50;
        private const DOOBER_GAP:Number = 15;
        private const DOOBER_DELAY:Number = 250;
        private const DOOBER_INITIAL_TIMEOUT:Number = 10000;
        private const DOOBER_FTUE_INITIAL_TIMEOUT:Number = 10000000;
        private const DOOBER_TIMEOUT_INCR:Number = 500;
        private const DOOBER_VELOCITY_RANGE:Number = 5;
        private const DOOBER_VELOCITY_MIN:Number = 10;

        protected var m_world:GameWorld;
        protected var m_doobers:Array;
        protected var m_batchRemaining:Dictionary;
        private var m_doobersEnabled:Boolean;
        private var m_lastDooberClearTime:Number = 0;

        public function DooberManager(_arg1:GameWorld){
            this.m_batchRemaining = new Dictionary();
            super();
            this.m_world = _arg1;
            _arg1.addObserver(this);
        }
        private static function get dooberBatchId():int{
            return (DooberManager.m_lastDooberBatchId++);
        }

        public function initialize():void{
            this.m_doobers = [];
            this.m_doobersEnabled = true;
        }
        public function get doobers():Array{
            return (this.m_doobers);
        }
        public function cleanUp():void{
            var _local1:Doober;
            if (this.m_doobers.length > 0){
                for each (_local1 in this.m_doobers) {
                    _local1.cleanUp();
                    _local1 = null;
                };
            };
            this.m_doobers = null;
        }
        public function getDooberCount():int{
            return (this.m_doobers.length);
        }
        public function areDoobersLanded():Boolean{
            var _local1:Doober;
            for each (_local1 in this.m_doobers) {
                if (_local1.getState() == Doober.STATE_INITIAL){
                    return (false);
                };
            };
            return (true);
        }
        public function onGameLoaded(_arg1:Object):void{
        }
        public function onResourceChange(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3):void{
        }
        protected function createDoober(_arg1:int, _arg2:MapResource, _arg3:String, _arg4:int, _arg5:Item, _arg6:Number, _arg7:Number, _arg8:Number, _arg9:Number, _arg10:Number, _arg11:int, _arg12:Number, _arg13:Function=null):Doober{
            var newDoober:* = null;
            var batchId:* = _arg1;
            var spawner:* = _arg2;
            var dooberName:* = _arg3;
            var amount:* = _arg4;
            var ownerItem:* = _arg5;
            var positionX:* = _arg6;
            var positionY:* = _arg7;
            var endPosX:* = _arg8;
            var endPosY:* = _arg9;
            var yVelocity:* = _arg10;
            var timeOut:* = _arg11;
            var spawnDelay:* = _arg12;
            var onCollectCallback = _arg13;
            newDoober = new Doober(spawner, dooberName, amount, ownerItem, new Point(positionX, positionY), new Point(endPosX, endPosY), yVelocity, timeOut, false, null, onCollectCallback, batchId);
            newDoober.addEventListener(Doober.CLEAR_EVENT, this.onDooberClear);
            var variant:* = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_DOOBER_TRACKING);
            if (variant){
                StatsManager.count("doobers", "doober_creation", "client", dooberName, amount.toString());
            };
            newDoober.setPosition(positionX, positionY);
            this.m_doobers.push(newDoober);
            TimerUtil.callLater(function ():void{
                newDoober.setOuter(Global.world);
                newDoober.attach();
                GlobalEngine.viewport.setDirty();
            }, spawnDelay);
            return (newDoober);
        }
        private function onDooberClear(_arg1:DooberEvent):void{
            var _local2:int = _arg1.batchId;
            this.m_batchRemaining[_local2].batchRemaining--;
            if (this.m_batchRemaining[_local2].batchRemaining <= 0){
                if (this.m_batchRemaining[_local2].callback != null){
                    (this.m_batchRemaining[_local2].callback as Function)();
                };
                delete this.m_batchRemaining[_local2];
            };
        }
        public function createBatchDoobers(_arg1:Array, _arg2:Item, _arg3:Number, _arg4:Number, _arg5:Boolean=false, _arg6:Function=null):void{
            var _local16:Point;
            var _local17:Number;
            var _local18:Number;
            var _local19:Number;
            var _local20:Point;
            var _local21:Doober;
            var _local7:int = dooberBatchId;
            if (((((!(this.m_doobersEnabled)) || ((_arg1 == null)))) || ((_arg1.length <= 0)))){
                return;
            };
            this.m_batchRemaining[_local7] = {
                batchRemaining:_arg1.length,
                callback:_arg6
            };
            var _local8:Number = _arg1.length;
            var _local9:Number = ((this.DOOBER_ELLIPSE_A * 2) / _local8);
            var _local10:int = this.DOOBER_INITIAL_TIMEOUT;
            if (Global.guide.isActive()){
                _local10 = this.DOOBER_FTUE_INITIAL_TIMEOUT;
            };
            var _local11:Number = 0;
            var _local12:Number = -1;
            var _local13:Number = 0;
            var _local14:Boolean;
            var _local15:int;
            while (_local15 < _local8) {
                if (_arg1[_local15][1] > 0){
                    _local14 = true;
                    _local16 = IsoMath.viewportToStage(IsoMath.tilePosToPixelPos(_arg3, _arg4));
                    _local17 = ((Math.random() * _local9) + ((_local16.x - this.DOOBER_ELLIPSE_A) + _local11));
                    if ((_local17 - _local12) < this.DOOBER_GAP){
                        if ((_local17 + this.DOOBER_GAP) <= (_local16.x + this.DOOBER_ELLIPSE_A)){
                            _local17 = (_local17 + this.DOOBER_GAP);
                        } else {
                            _local17 = (_local16.x + this.DOOBER_ELLIPSE_A);
                            _local11 = -(_local9);
                        };
                    };
                    _local18 = this.getEndEllipticalPos(_local16, _local17, this.DOOBER_ELLIPSE_A, (this.DOOBER_ELLIPSE_A / 2));
                    _local19 = (-1 * ((Math.random() * this.DOOBER_VELOCITY_RANGE) + this.DOOBER_VELOCITY_MIN));
                    _local20 = IsoMath.screenPosToTilePos(_local17, _local18);
                    _local21 = this.createDoober(_local7, _arg1[_local15][2], _arg1[_local15][0], Math.ceil(_arg1[_local15][1]), _arg2, _arg3, _arg4, _local20.x, _local20.y, _local19, _local10, _local13);
                    if (_arg5){
                        _local21.isStreanBonusEnabled = false;
                    };
                    _local11 = (_local11 + _local9);
                    _local12 = _local17;
                    _local13 = (_local13 + this.DOOBER_DELAY);
                    _local10 = (_local10 + this.DOOBER_TIMEOUT_INCR);
                };
                _local15++;
            };
            if (_local14){
                Sounds.play("doober_drop");
            };
        }
        public function removeDoober(_arg1:Doober):void{
            if (!this.m_doobers){
                return;
            };
            var _local2:int = this.m_doobers.indexOf(_arg1);
            if (_local2 >= 0){
                this.m_doobers.splice(_local2, 1);
            };
            if (this.m_doobers.length == 0){
                this.m_lastDooberClearTime = GlobalEngine.getTimer();
            };
            if (this.m_lastDooberClearTime != 0){
                this.m_lastDooberClearTime = 0;
                GameTransactionManager.addTransaction(new TUpdateEnergy(), true);
            };
        }
        public function autoCollect():void{
            var _local2:Doober;
            if (!this.m_doobers){
                return;
            };
            var _local1:Array = ArrayUtil.copyArray(this.m_doobers);
            for each (_local2 in _local1) {
                _local2.collectIfIdle();
            };
        }
        public function createDummyDoober(_arg1:String, _arg2:Point, _arg3:Point):void{
            var _local4:Doober = new Doober(null, _arg1, 0, null, _arg2, null, 0, 0, true, _arg3);
            var _local5:Point = IsoMath.screenPosToTilePos(_arg2.x, _arg2.y);
            _local4.setPosition(_local5.x, _local5.y);
            this.m_doobers.push(_local4);
            _local4.setOuter(Global.world);
            _local4.attach();
        }
        public function toogleEnableDoobers():void{
            this.m_doobersEnabled = !(this.m_doobersEnabled);
        }
        public function isDoobersEnabled():Boolean{
            return (this.m_doobersEnabled);
        }
        protected function getEndPosYAroundBuilding(_arg1:Point, _arg2:Number, _arg3:Number):Number{
            return ((Math.sqrt((Math.pow(_arg2, 2) - Math.pow((_arg3 - _arg1.x), 2))) + _arg1.y));
        }
        protected function getEndEllipticalPos(_arg1:Point, _arg2:Number, _arg3:Number, _arg4:Number):Number{
            return ((Math.sqrt((((Math.pow(_arg3, 2) * Math.pow(_arg4, 2)) - (Math.pow(_arg4, 2) * Math.pow((_arg2 - _arg1.x), 2))) / Math.pow(_arg3, 2))) + _arg1.y));
        }

    }
}//package Classes.doobers 
